package com.hlx.controller;

import com.hlx.enumeration.TypeEnum;
import com.hlx.service.ChatHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController {

	private final ChatClient chatClient;

	private final ChatHistoryService chatHistoryService;

	/**
	 * 设置返回类型为text/html，编码为utf-8
	 * @param prompt
	 * @return
	 */
	@GetMapping(value = "/chat", produces = "text/html; charset=utf-8")
	public Flux<String> chat(String prompt, String chatId){
		// 保存会话id
		chatHistoryService.save(TypeEnum.CHAT.getValue(), chatId);
		// 调用模型
		return chatClient
				.prompt() // 创建一个prompt
				.user(prompt) // 设置用户输入
				.advisors(a -> a.param(ChatMemory.CONVERSATION_ID, chatId))
				.stream() // 流式返回
				.content(); // 获取内容
	}
}
